package com.android.pos;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.android.pos.db.POSDbAdapter;

public class NewOrder extends Activity implements OnClickListener {
	private static final int ACTIVITY_CREATE=0;
	private static final int ACTIVITY_EDIT=1;
	
	private static final int INSERT_ID = Menu.FIRST;
	private static final int DELETE_ID = Menu.FIRST + 1;
	  
    private static final String Flag = "NewOrder";
    private static final String TAG = "NewOrder";   
    final static int FOOD_SELECT = 0;
    
    private Button BtnFood;
    private Button BtnPlus;
    
    // dynamicArea
    private final int DYNAMIC_VIEW_TVIEW_ID = 0x8000;
    private final int DYNAMIC_VIEW_SPINER_ID = 0x8000;
    private final int DYNAMIC_VIEW_BTN_ID = 0x8000;
    
    private LinearLayout dynamicArea;
    private ListView	dynamicLView;
    private int numButton;
    private int numTxtEdt;
    
    // DB 조작하는 클래스 로드
    private POSDbAdapter mDbHelperTable;
    private POSDbAdapter mDbHelperItem;    
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d(Flag,"");          
        Log.d(Flag,">>> first loading or refresh NewOrder...");
        
        super.onCreate(savedInstanceState);      
        setContentView(R.layout.neworder);

        
        // Initialize DB
        mDbHelperTable = new POSDbAdapter(this);
        mDbHelperTable.open();
        
        Log.d(Flag,">>> DataBase ready to servce...");        
        
        // Get spinner DB and draw
        drawAvailableTableSpinner();
        
        // Create food selection button 
        BtnFood = (Button)findViewById(R.id.btn_itemselection);
        BtnFood.setOnClickListener(new Button.OnClickListener() {

			public void onClick(View arg0) {
	        	Context context = getApplicationContext();        	
				Log.d(Flag,"It's about to open food selection Activity");
	    		Intent intent = new Intent(NewOrder.this, FoodSelection.class);
	    		// Could you return food/price/qty as a array? 
	    		startActivityForResult(intent, FOOD_SELECT);
//	    		startActivity(intent);	        	
				
			}        
        });
        
        // Create "+" button 
        BtnPlus = (Button)findViewById(R.id.btn_addmenu);
        BtnPlus.setOnClickListener(new Button.OnClickListener() {

			public void onClick(View arg0) {
	        	Context context = getApplicationContext();        	
				Log.d(Flag,"Add 1 row to below table");
				
//				dynamicArea = (LinearLayout)findViewById(R.id.dynamicArea);
				dynamicLView = (ListView)findViewById(R.id.dynamicArea);
				addTextView();
			}
        });
        
    }
    
    private void addTextView() {
    	numTxtEdt++;
    	
    	// Add TextView
    	TextView txtedt1 = new TextView(this);
    	txtedt1.setId(DYNAMIC_VIEW_TVIEW_ID + numTxtEdt);
    	txtedt1.setText("test" + numTxtEdt);
    	txtedt1.setOnClickListener(this);
    	
    	// Add delete button
    	final Button btn_del	= new Button(this);
    	btn_del.setId(DYNAMIC_VIEW_BTN_ID + numTxtEdt);
    	btn_del.setText("Del");
    	btn_del.setOnClickListener(new Button.OnClickListener() {

			public void onClick(View arg0) {
	        	//Context context = getApplicationContext();        	
				Log.d(Flag,"delete buttn clicked!!!!!");
				Log.d(Flag,"test..." + btn_del.getId());
				
			}     
    	});
    	
    	// Add Spiner
    	Spinner spiner1 = new Spinner(this);
    	int spinner_id = DYNAMIC_VIEW_SPINER_ID + numTxtEdt;
    	spiner1.setId(spinner_id);
    	
    	// initialization for DB connection
		mDbHelperItem = new POSDbAdapter(this);
		mDbHelperItem.open();
		
		// 모든 아이템(메뉴)를 갖고와서 커서에 저장
		Cursor itemCursor = mDbHelperItem.fetchAllItems();
		// 안드로이드 life cycle 관리를 위한 커서 매니징
		startManagingCursor(itemCursor);
		
		ArrayList<String> mArrayList = new ArrayList<String>();
		itemCursor.moveToFirst();
		while(!itemCursor.isAfterLast()) {
		     mArrayList.add(itemCursor.getString(itemCursor.getColumnIndex("item")));
		     itemCursor.moveToNext();
		}
		
        // Creating adapter for spinner
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, mArrayList);
 
        // Drop down layout style - list view with radio button
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
 
        // attaching data adapter to spinner
        spiner1.setAdapter(dataAdapter);
    	
    	dynamicArea.addView(txtedt1, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    	dynamicArea.addView(spiner1, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    	dynamicArea.addView(btn_del, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    	
    }
    
    
//    private void pushButton() {
//    	numButton++;
//    	
//    	Button dynamicButton = new Button(this);	
//    	dynamicButton.setId(DYNAMIC_VIEW_ID + numButton);
//    	dynamicButton.setText("button" + numButton);
//    	dynamicButton.setOnClickListener(this);
//    	
//    	dynamicArea.addView(dynamicButton, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
//    	
//    	
//    }
    
    public void drawAvailableTableSpinner(){
	  // Get all of the rows from the database and create the item list
	  Cursor itemCursor = mDbHelperTable.fetchAllAvailTables();
	  startManagingCursor(itemCursor);
				Log.d(TAG, "Ready to start managing cursor...");
				Log.d(TAG, "cursor..."+itemCursor.getCount());
	
	  List<String> list = new ArrayList<String>();
	  
	  // create the string array.
	  while(itemCursor.moveToNext()){
	      String eng = itemCursor.getString(0);
	      list.add(eng);
	  }				
	  
	  Spinner s = (Spinner) findViewById(R.id.spinner_table);

	  ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
			  android.R.layout.simple_spinner_item, list);
	  dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	  s.setAdapter(dataAdapter);
		
    }

	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		
	}
    

}
